package com.bw.services.IM

import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bw.services.R
import com.bumptech.glide.Glide

class MessageListAdapter(
    private val messages: List<ChatMessage>,
    private val selfUsername: String
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        private const val TYPE_LEFT = 0
        private const val TYPE_RIGHT = 1
    }

    override fun getItemViewType(position: Int): Int {
        return if (messages[position].isSelf) TYPE_RIGHT else TYPE_LEFT
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == TYPE_LEFT) {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.item_message_left, parent, false)
            LeftViewHolder(view)
        } else {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.item_message_right, parent, false)
            RightViewHolder(view)
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val msg = messages[position]
        if (holder is LeftViewHolder) {
            holder.bind(msg)
        } else if (holder is RightViewHolder) {
            holder.bind(msg)
        }
    }

    override fun getItemCount(): Int = messages.size

    class LeftViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tvMessage: TextView = itemView.findViewById(R.id.tv_message)
        private val ivImage: ImageView = itemView.findViewById(R.id.iv_image)
        fun bind(msg: ChatMessage) {
            if (msg.type == ChatMessage.Type.TEXT) {
                tvMessage.visibility = View.VISIBLE
                tvMessage.text = msg.text
                ivImage.visibility = View.GONE
            } else if (msg.type == ChatMessage.Type.IMAGE) {
                tvMessage.visibility = View.GONE
                ivImage.visibility = View.VISIBLE
                // 新增：对方发来的图片用Glide加载
                Glide.with(itemView.context).load(msg.imageUrl).into(ivImage)
            }
        }
    }

    class RightViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tvMessage: TextView = itemView.findViewById(R.id.tv_message)
        private val ivImage: ImageView = itemView.findViewById(R.id.iv_image)
        fun bind(msg: ChatMessage) {
            if (msg.type == ChatMessage.Type.TEXT) {
                tvMessage.visibility = View.VISIBLE
                tvMessage.text = msg.text
                ivImage.visibility = View.GONE
            } else if (msg.type == ChatMessage.Type.IMAGE) {
                tvMessage.visibility = View.GONE
                ivImage.visibility = View.VISIBLE
                ivImage.setImageURI(Uri.parse(msg.imageUrl))
            }
        }
    }
} 